package com.koron.css2.baseConfig.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.bean.ContractTemplateBean;
import com.koron.css2.baseConfig.mapper.ContractTemplateMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.Map;

public class ContractTemplateQuery implements ServerInterface{

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		@SuppressWarnings("rawtypes")
        MessageBean<ContractTemplateBean> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", ContractTemplateBean.class);

        try {
            ContractTemplateMapper mapper = factory.getMapper(ContractTemplateMapper.class);
            Map map = JsonUtils.objectToPojo(req.getData(), Map.class);
            if (StringUtils.isEmpty(map.get("id").toString())) {
                return MessageBean.create(Constant.ILLEGAL_PARAMETER, "id不能为空", void.class);
            }
            ContractTemplateBean contractTemplateBean = mapper.selectById(map.get("id").toString());
            Map<String, String> signType = Tools.mapDicByCode(factory, "signType");
            Map<String, String> type_contract = Tools.mapDicByCode(factory, "type_contract");

                if(contractTemplateBean.getSignType()!=null){
                    contractTemplateBean.setSignTypeName(signType.get(contractTemplateBean.getSignType()).trim());
                }
                if(contractTemplateBean.getTypeContract()!=null){
                    contractTemplateBean.setTypeContractName(type_contract.get(contractTemplateBean.getTypeContract()).trim());
                }


            info.setData(contractTemplateBean);
        } catch (Exception e) {
            info.setCode(Constant.MESSAGE_INT_FAIL);
            info.setDescription(e.getMessage());
            logger.error(e.getMessage(), e);
        }
        return info;
	}

}
